#include <iostream>
using namespace std;


/**
 * 时间复杂度 O(n)
 * 空间复杂度 O(1)
 * 
 * 报数游戏 
 * 
 */
int main() {
    int a[60];

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        if (a[i] % 3 == 0) {
            cout << a[i] << " ";
        }
    }
    cout << endl;
    for (int i = 0; i < n; i++) {
        if (a[i] % 5 == 0) {
            cout << a[i] << " ";
        }
    }
    cout << endl;
    for (int i = 0; i < n; i++) {
        if (a[i] % 7 == 0) {
            cout << a[i] << " ";
        }
    }
    cout << endl;
    return 0;
}